//
//  Bader.m
//  iPhoneRegister
//
//  Created by Devon Buchanan on 02/July/2009.
//  Copyright 2009 divinenephron. All rights reserved.
//

#import "Bader.h"
#import "ASIHTTPRequest.h"
#import <Foundation/Foundation.h>

//static NSString *kNTLMUsername = @"NTLMUsername";
//static NSString *kNTLMPassword = @"NTLMPassword";
static NSString *kSMSUsername = @"SMSUsername";
static NSString *kSMSPassword = @"SMSPassword";

@implementation Bader

#pragma mark -
#pragma mark Creation

static Bader *SharedInstance;

+ (Bader *)sharedInstance
{
	return SharedInstance ?: [[self new] autorelease];
}

- (id)init
{
	if(SharedInstance)
	{
		[self release];
	}
	else if(self = SharedInstance = [[super init] retain])
	{   
        urlQueue = [[NSOperationQueue alloc] init];
        
        // Create the request object to get the bader login page
        NSURL *url = [NSURL URLWithString:@"https://sms.bader.mod.uk/Login.aspx?ReturnUrl=%2fsqn%2freminders.aspx"];
        ASIHTTPRequest *request = [[[ASIHTTPRequest alloc] initWithURL:url] autorelease];
        
        // Tell it the NTLM username, password and domain.
        [request setUsername:@"<1234>"];
        [request setPassword:@"<Password>"];
        [request setDomain:@"<domain.com>"]; // I'm not too sure whether this has to be in allcaps or not
        
        // Tell it how to inform me of when it is finished
        [request setDelegate:self];
        [request setDidFinishSelector:@selector(requestDone:)];
        [request setDidFailSelector:@selector(requestWentWrong:)];
        
        // Get the url
        [urlQueue addOperation:request]; //queue is an NSOperationQueue
	}
	return SharedInstance;
}

- (void)dealloc {
    [urlQueue release];
    [super dealloc];
}

#pragma mark -
#pragma mark ASIHTTPRequest Delegate Methods


- (void)requestDone:(ASIHTTPRequest *)request
{
    NSLog(@"%@", [request responseString]);
}

- (void)requestWentWrong:(ASIHTTPRequest *)request
{
    NSLog(@"%@", [request error]);
}

#pragma mark -
#pragma mark Authenication Getter Methods

// FIXME: This should use the keychain to store the passwords, it is encrypted unlike defaults.

static NSString *smsHost = @"sms.bader.mod.uk";
static NSInteger smsPort = 443;
static NSString *smsProtocol = @"HTTPS";
static NSString *smsRealm = nil;

- (NSString *)NTLMUsername {
    NSURLCredential *credential = [ASIHTTPRequest savedCredentialsForHost:smsHost
                                                                        port:smsPort
                                                                    protocol:smsProtocol
                                                                    realm:smsRealm];
    return credential.user;
    
//    return [[NSUserDefaults standardUserDefaults] stringForKey:kNTLMUsername];
}

- (NSString *)NTLMPassword {
    NSURLCredential *credential = [ASIHTTPRequest savedCredentialsForHost:smsHost
                                                                     port:smsPort
                                                                 protocol:smsProtocol
                                                                    realm:smsRealm];
    return credential.password;
//    return [[NSUserDefaults standardUserDefaults] stringForKey:kNTLMPassword];
}

- (NSString *)SMSUsername {
    return [[NSUserDefaults standardUserDefaults] stringForKey:kSMSUsername];
}

- (NSString *)SMSPassword {
    return [[NSUserDefaults standardUserDefaults] stringForKey:kSMSPassword];
}

#pragma mark -
#pragma mark Authenication Setter Methods
// Save creddentials to the keychain
//+ (void)saveCredentials:(NSURLCredential *)credentials forHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm;
//
// Return credentials from the keychain
//+ (NSURLCredential *)savedCredentialsForHost:(NSString *)host port:(int)port protocol:(NSString *)protocol realm:(NSString *)realm;

- (void)setNTLMUsername:(NSString *)newUsername {
    NSURLCredential *oldCredential = [ASIHTTPRequest savedCredentialsForHost:smsHost
                                                                        port:smsPort
                                                                    protocol:smsProtocol
                                                                       realm:smsRealm];
    [ASIHTTPRequest saveCredentials:[NSURLCredential credentialWithUser:newUsername
                                                               password:oldCredential.password
                                                            persistence:NSURLCredentialPersistencePermanent]
                            forHost:smsHost
                               port:smsPort
                           protocol:smsProtocol
                              realm:smsRealm];
//    [[NSUserDefaults standardUserDefaults] setObject:newValue forKey:kNTLMUsername];
}

- (void)setNTLMPassword:(NSString *)newPassword {
    NSURLCredential *oldCredential = [ASIHTTPRequest savedCredentialsForHost:smsHost
                                                                        port:smsPort
                                                                    protocol:smsProtocol
                                                                       realm:smsRealm];
    [ASIHTTPRequest saveCredentials:[NSURLCredential credentialWithUser:oldCredential.user
                                                               password:newPassword
                                                            persistence:NSURLCredentialPersistencePermanent]
                            forHost:smsHost
                               port:smsPort
                           protocol:smsProtocol
                              realm:smsRealm];
//    [[NSUserDefaults standardUserDefaults] setObject:newValue forKey:kNTLMPassword];
}

- (void)setSMSUsername:(NSString *)newValue {
    [[NSUserDefaults standardUserDefaults] setObject:newValue forKey:kSMSUsername];
}

- (void)setSMSPassword:(NSString *)newValue {
    [[NSUserDefaults standardUserDefaults] setObject:newValue forKey:kSMSPassword];
}

@end
